from django.db import models


# Create your models here.
# from django.db import migrations,models
# class Migration(migrations.Migration):
#     initial = True
#     dependencies = [
#
#     ]
#     operations = [
#         migrations.CreateModel(
#             name='BookInfo',
#             fields=[
#                 ('id',models.AutoField(auto_created=True,primary_key=True,serialize=False,verbose_name='ID')),
#                 ('name',models.CharField(max_length=20,verbose_name='名称')),
#                 ('pub_date',models.DateField(null=True,verbose_name='发布日期')),
#                 ('readcount',models.IntegerField(default=0,verbose_name='阅读量')),
#                 ('commentcount',models.IntegerField(default=0,verbose_name='评论量')),
#                 ('is_delete',models.BooleanField(default=False,verbose_name='逻辑删除')),
#
#             ],
#         ),
#     ]
# 自定义管理器
class CountryManager(models.Manager):
    def cotry_name_prefix(self):
        all_countries = self.all()
        for country in all_countries:
            country.country_name = '国家：' + country.country_name

        return all_countries


class BookInfo(models.Model):
    name = models.CharField(max_length=20, verbose_name="名称")
    pub_date = models.DateField(verbose_name="发布日期")
    readcount = models.IntegerField(default=0, verbose_name="阅读量")
    commentcount = models.IntegerField(default=0, verbose_name="评论量")
    is_delete = models.BooleanField(default=False, verbose_name="逻辑删除")

    def __str__(self):
        return self.name


class Country:
    pass


class Country(models.Model):
    country_code = models.CharField(max_length=20)
    country_name = models.CharField(max_length=50)
    objects = CountryManager()

    # 后期如何使用
    # Country.objects.country_name_prefix()
    class Meta:
        db_table = "country"
        verbose_name = '国家'
        verbose_name_plual = '国家'


class PersonManager(models.Manager):
    def get_queryset(self):
        return super(PersonManager, self).get_queryset().filter(person_nation_exact=1)


class Person(models.Model):
    property_name = models.CharField(max_length=20)
    property_age = models.IntegerField(default=0)
    property_money = models.IntegerField(default=0)
    property_nation = models.ForeignKey(Country, on_delete=models.CASCADE)

    objects = PersonManager()

    # Person.objects.get_queryset()
    class Meta:
        db_table = "person"
        verbose_name = '人民'
        verbose_name_plual = '人民'


def __str__(self):
    return self.name


class Teacher(models.Model):
    name = models.CharField(max_length=10)

    class Meta:
        db_table = 'teacher'
        verbose_name = '教师'
        verbose_name_plual = '教师'


class Student(models.Model):
    name = models.CharField(max_length=10)
    teachers = models.ManyToManyField(Teacher)

    class Meta:
        db_table = 'students'
        verbose_name = '学生'
        verbose_name_plual = '学生'


class President(models.Model):
    president_name = models.CharField(max_length=10)
    president_gender = models.CharField(max_length=10)
    president_nation = models.OneToOneField(Country, on_delete=models.CASCADE)

    class Meta:
        db_table = 'presidents'
        verbose_name = '总统'
        verbose_name_plual = '总统'
